#include <stdio.h>
#include <stdint.h>
#include "ff.h"
int main()
{
    FIL         fp;
    FATFS       ffs;
    uint32_t    len;
    char        buf[255];
    const char *path = "0:test.txt";

    strcpy(buf, "SDC test");

    //mount ffs work area
    if (f_mount(0, &ffs) != 0) { 
        printf("f_mount() failed\n");
        goto error;
    }

    //open file for writing
    if (f_open(&fp, path, FA_WRITE|FA_CREATE_ALWAYS) != 0) { 
        printf("f_open() failed\n");
        goto error;
    }
    
    //write
    if (f_write(&fp, buf, strlen(buf), &len) != FR_OK) {
        printf("f_read() failed \n");
        goto error_rw;
    }

    f_close(&fp);
    printf("finished writing '%s'\n", buf);

    //open file for reading
    if (f_open(&fp, path, FA_READ|FA_OPEN_EXISTING) != 0) { 
        printf("f_open() failed\n");
        goto error;
    }
    
    //read
    if (f_read(&fp, buf, sizeof(buf), &len) != FR_OK) {
        printf("f_read() failed \n");
        goto error_rw;
    }

    buf[len] = 0;
    printf("finished reading '%s'\n", buf);
error_rw:
    f_close(&fp);
error:
    while(1){
        sleep(1000);
    }
    return 0;
}
